﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PlayNote.Model;
using PlayNugetPackage.DBClass;

namespace PlayNote.Bll
{
    public class NoteTableBll
    {
        private readonly static IADOHelper ado;
        static string TableName = "NoteTable";
        static NoteTableBll()
        {
            string constr =string.Format( "server={0};Initial Catalog={1};UID={2};PWD={3}","","","","");
            constr = $"server={"."};Initial Catalog={"PlayNote"};UID={"sa"};PWD={"sa123"}";
            ado = ADOHelper.GetADO(constr,DBType.SqlServer);
        }
        public static List<NoteTable> GetAll(bool hasDel=false)
        {
            List<NoteTable> result=new List<NoteTable>();
            string sql = $"select * from {TableName} ";
            if (!hasDel)
            {
                sql = $"select * from {TableName} where IsDeleted='{false}'";
            }
            var da= ado.SelectBySql(sql);
            if (da != null) 
            { 
                return ADOHelper.DataSetToList<NoteTable>(da);
            }
            return result;
        }
        public static bool InsertNote(string msg)
        {
            NoteTable model=new NoteTable();
            model.UpdateTime=DateTime.Now;
            model.Msg = msg;
            model.IsDeleted = false;
            model.CreateTime = DateTime.Now;
            return ADOHelper.Insert<NoteTable>(model, ado)>0;
        }
        public static bool DeleteNote(int id) 
        {
            string sql = $"update {TableName}  set IsDeleted=1 where Id={id}";
            return ado.ExecuteSql(sql) > 0;
        }

        public static bool UpdateNote(int id,string msg) 
        {
            List<Tuple<string,object>> tupls = new List<Tuple<string,object>>();
            tupls.Add(new Tuple<string, object>("msg", msg));
            string sql = $"update {TableName}  set UpdateTime='{DateTime.Now}',Msg=@msg where Id={id}";
            return ado.ExecuteSqlUsePars(sql, tupls) > 0;
        }
    }
}
